<template>
  <div>
    <el-dialog v-model="dialogVisible" title="Tips" width="50%" :before-close="handleClose">
      <el-form :model="form">
        <el-form-item>
          <Editor v-model="form.content" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">Cancel</el-button>
          <el-button type="primary" @click="dialogVisible = false">Confirm</el-button>
        </span>
      </template>
    </el-dialog>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i>
          评论信息
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <!-- <div class="handle-box">
        <el-select v-model="query.type" placeholder="评论类型" class="handle-select mr10">
          <el-option key="0" label="景点" value="0"></el-option>
          <el-option key="1" label="文章" value="1"></el-option>
          <el-option key="2" label="用户" value="2"></el-option>
        </el-select>
        <el-input v-model="query.username" placeholder="用户名" class="handle-input mr10"></el-input>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
      </div> -->
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
      >
        <el-table-column prop="commentId" label="评论ID" width="55" align="center"></el-table-column>
        <el-table-column prop="attractionId" label="景点" width="80"></el-table-column>
        <el-table-column prop="articleId" label="文章" width="80"></el-table-column>
        <el-table-column prop="userId" label="用户" width="80"></el-table-column>
        <el-table-column label="评论类型" width="100" align="center">
          <template #default="scope">
            <el-tag
              :type="
                scope.row.commentCategoryce == '0'
                  ? 'danger'
                  : scope.row.commentCategoryce == '1'
                  ? 'success'
                  : 'warning'
              "
            >
              {{
              scope.row.commentCategoryce == "0"
              ? "景点"
              : "文章"
              }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="commentDesc" label="评论内容"></el-table-column>
        <el-table-column label="状态" align="center" width="80">
          <template #default="scope">
            <el-tag
              :type="
                scope.row.commenttype == '1'
                  ? 'success'
                  : scope.row.commenttype == '0'
                  ? 'danger'
                  : ''
              "
            >
              {{
              scope.row.commenttype == "1"
              ? "正常"
              :"隐藏"
              }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column prop="createdAt" label="评论时间"></el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template #default="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
            >隐藏</el-button>
            <el-button
              type="text"
              icon="el-icon-delete"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination v-if="totalCount" v-model:currentPage="query.currentPage" v-model:page-size="query.pageSize"
          layout="prev, pager, next, jumper" :total="totalCount" @current-change="handleCurrentChange" />
      </div>
    </div>
  </div>
</template>
<script>
import { ref, reactive } from "vue";
import { ElMessageBox } from "element-plus";
import request from "../../utils/request";
import Editor from "@/components/Editor.vue";
export default {
  components: {
    Editor,
  },
  setup() {
    const tableData = ref([]);
    const pageTotal = ref(0);
    const dialogVisible = ref(false);
     const totalCount = ref(0);
    const form = reactive({
      content: "",
    });
    const query = reactive({
      name: "",
      pageIndex: 1,
      currentPage: 1,
      pageSize: 5,
    });

    // 查询操作
    const handleSearch = () => {
      console.log(query.type)
      getData();
    };
    // 分页导航
    const handleCurrentChange = (val) => {
       query.currentPage = val;
       console.log(query.currentPage);
        getData();
    };
    // 删除操作
    const handleDelete = (index,row) => {

       request
        .request({
          url: "/comment/del",
          method: "get",
          params: {
             commentId:row.commentId
          },
        })
        .then((res) => {
          if (res.data.msg == "success") {
            getData();
            ElNotification({
              title: "删除成功",
              message: "成功删除评论信息",
            });
          } else {
            ElNotification({
              title: "删除失败",
              message: "请联系管理员检查失败原因",
            });
          }
        })
        .catch((e) => {
          ElNotification({
            title: "删除失败",
            message: "请联系管理员检查失败原因",
          });
        });
    };
    //修改操作
    const handleEdit = (index,row) => {
      // dialogVisible.value = true;
      // console.log(index, dialogVisible);
      row.commenttype=row.commenttype=='1'?'0':'1';
       request
        .request({
          url:"/comment/altercomment",
          data:{
            commentId:row.commentId,
            commenttype:row.commenttype
          },
          method:"post",
        })
        .then((res) => {
          
        console.log(res)
         
        })
     
    };
    const handleClose = (done) => {
      ElMessageBox.confirm("Are you sure to close this dialog?")
        .then(() => {
          done();
        })
        .catch(() => {
          // catch error
        });
    };
    
     const getData = () => {  
      
      request
        .request({
          url: "/comment/getAllCommentByPage",
          params: {
            type: query.type,
            username: query.username,
            currentPage: query.currentPage,
            pageSize: query.pageSize,
          },
          method: "get",
        })
        .then((res) => {
          //获取当前页的数据
          tableData.value = res.data.data.rows;
          //获取总记录数
          getUserName(tableData.value)
          getAttractionName(tableData.value)
          getArticleName(tableData.value)
          totalCount.value = res.data.data.count;
        })
        .catch((e) => {
          console.log(e);
        });
    };
    const getUserName=(e)=>{
      //获取用户
      e.forEach(function(item,index)  {
        request
        .request({
          url:"/user/getUsersById",
          params:{
            userId:e[index].userId,
          },
          method:"get",
        })
        .then((res) => {
          e[index].userId=res.data.data.userName
        })
       })
      }
   const getAttractionName=(e)=>{
      //获取景点
      e.forEach(function(item,index) {
        request
        .request({
          url:"/attraction/getAttractionsById",
          params:{
            attractionId:e[index].attractionId,
          },
          method:"get",
        })
        .then((res) => {
          e[index].attractionId=res.data.data.attractionName
         
        })
       })
      }
       
      //获取文章名字
      const getArticleName=(e)=>{
       
      e.forEach(function(item,index) {
         console.log(e[index])
        request
        .request({
          url:"/article/getArticleById",
          params:{
            articleId:e[index].articleId,
          },
          method:"get",
        })
        .then((res) => {
          
          e[index].articleId=res.data.data.title
         
        })
       })
      }

    getData();
    return {
      query,
      pageTotal,
      tableData,
      dialogVisible,
      form,
      totalCount,
      getData,
      handleSearch,
      handleCurrentChange,
      handleDelete,
      handleEdit,
      handleClose,
    };
  },
};
</script>
<style lang="scss" scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}

.table {
  width: 100%;
  font-size: 14px;
}

.red {
  color: #ff0000;
}

.mr10 {
  margin-right: 10px;
}

.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}

.dialog-footer button:first-child {
  margin-right: 10px;
}
</style>